//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OptionletStripper.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Capfloor/CapFloorTermVolSurface.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/StrippedOptionletBase.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures::Volatility::Capfloor;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (boost::shared_ptr<QuantLib::OptionletStripper>& childNative, Object^ owner) : CStrippedOptionletBase(COptionletStripper::typeid)
{
#ifdef HANDLE
	_phOptionletStripper = NULL;
#endif
	_ppOptionletStripper = &childNative;
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppOptionletStripper));
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (QuantLib::OptionletStripper& childNative, Object^ owner) : CStrippedOptionletBase(COptionletStripper::typeid)
{
#ifdef HANDLE
	_phOptionletStripper = NULL;
#endif
	_ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (&childNative);
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppOptionletStripper));
    _OptionletStripperOwner = owner;
    _StrippedOptionletBaseOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (COptionletStripper^ copy) : CStrippedOptionletBase(COptionletStripper::typeid)
{
#ifdef HANDLE
	_phOptionletStripper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (copy->GetShared());
        _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppOptionletStripper));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (PLATFORM::Type^ t) : CStrippedOptionletBase(COptionletStripper::typeid)
{
#ifdef HANDLE
	_phOptionletStripper = NULL;
#endif
	if (!t->IsSubclassOf(COptionletStripper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (QuantLib::Handle<QuantLib::OptionletStripper>& childNative, Object^ owner)  : CStrippedOptionletBase(COptionletStripper::typeid)
{
	_phOptionletStripper = &childNative;
	_ppOptionletStripper = &static_cast<boost::shared_ptr<QuantLib::OptionletStripper>>(childNative.currentLink());
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppOptionletStripper));
    _OptionletStripperOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (QuantLib::Handle<QuantLib::OptionletStripper> childNative)  : CStrippedOptionletBase(COptionletStripper::typeid)
{
	_phOptionletStripper = &childNative;
	_ppOptionletStripper = &static_cast<boost::shared_ptr<QuantLib::OptionletStripper>>(childNative.currentLink());
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppOptionletStripper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::COptionletStripper (QuantLib::OptionletStripper childNative)  : CStrippedOptionletBase(COptionletStripper::typeid)
{
#ifdef HANDLE
	_phOptionletStripper = NULL;
#endif
	_ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (new QuantLib::OptionletStripper (childNative));
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppOptionletStripper));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::~COptionletStripper ()
{
    if (_ppOptionletStripper != NULL)
    {
	    delete _ppOptionletStripper;
        _ppOptionletStripper = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::!COptionletStripper ()
{
    if (_ppOptionletStripper != NULL)
    {
	    delete _ppOptionletStripper;
    }
}
QuantLib::OptionletStripper& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::GetReference ()
{
    if (_ppOptionletStripper == NULL) throw REFNEW NativeNullException ();
	return **_ppOptionletStripper;
}
boost::shared_ptr<QuantLib::OptionletStripper>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::GetShared ()
{
    if (_ppOptionletStripper == NULL) throw REFNEW NativeNullException ();
	return *_ppOptionletStripper;
}
QuantLib::OptionletStripper* Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::GetPointer ()
{
    if (_ppOptionletStripper == NULL) throw REFNEW NativeNullException ();
	return &**_ppOptionletStripper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OptionletStripper>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::GetHandle ()
{
	if (_phOptionletStripper == NULL)
	{
		_phOptionletStripper = new Handle<QuantLib::OptionletStripper> (*_ppOptionletStripper);
	}
	return *_phOptionletStripper;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::HasNative () 
{
	return (_ppOptionletStripper != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::AtmOptionletRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppOptionletStripper)->atmOptionletRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::BusinessDayConventionEnum Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::BusinessDayConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppOptionletStripper)->businessDayConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppOptionletStripper)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppOptionletStripper)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IIborIndex^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::IborIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::IborIndex> _rv = (boost::shared_ptr<QuantLib::IborIndex>)(*_ppOptionletStripper)->iborIndex ( );   
        Cephei::QL::Indexes::CIborIndex^ _nrv = REFNEW Cephei::QL::Indexes::CIborIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletAccrualPeriods::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppOptionletStripper)->optionletAccrualPeriods ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<DateTime>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletFixingDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppOptionletStripper)->optionletFixingDates ( );   
        Cephei::Core::IVector<DateTime>^ _nrv = REFNEW CoVector<DateTime> (REFNEW CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletFixingTenors::get ()
{
    try
    {
    	std::vector<QuantLib::Period>& _rv = (std::vector<QuantLib::Period>&)(*_ppOptionletStripper)->optionletFixingTenors ( );   
        CoVector<Cephei::QL::Times::IPeriod^>^ _nrv = REFNEW CoVector<Cephei::QL::Times::IPeriod^>(REFNEW CPeriodVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletFixingTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppOptionletStripper)->optionletFixingTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletMaturities::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppOptionletStripper)->optionletMaturities ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<DateTime>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletPaymentDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppOptionletStripper)->optionletPaymentDates ( );   
        Cephei::Core::IVector<DateTime>^ _nrv = REFNEW CoVector<DateTime> (REFNEW CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletStrikes (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppOptionletStripper)->optionletStrikes ( _i );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::OptionletVolatilities (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	std::vector<QuantLib::Volatility>& _rv = (std::vector<QuantLib::Volatility>&)(*_ppOptionletStripper)->optionletVolatilities ( _i );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppOptionletStripper)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Capfloor::ICapFloorTermVolSurface^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper::TermVolSurface::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::CapFloorTermVolSurface> _rv = (boost::shared_ptr<QuantLib::CapFloorTermVolSurface>)(*_ppOptionletStripper)->termVolSurface ( );   
        Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolSurface^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::Capfloor::CCapFloorTermVolSurface (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

